package com.tojabo.swing.seventeen_thead;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

public class InvokeAndWaitDemo {

	public static void main(String[] args) {
		try {
			SwingUtilities.invokeAndWait(new Runnable() {
				public void run() {
					new mainframe();
				}
			});

		} catch (Exception e) {
		}
	}

}

class mainframe extends Thread {
	static final int WIDTH = 700;
	static final int HEIGHT = 400;

	mainframe() {
		JFrame frame = new JFrame("SwingWorker测试程序");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel pane = new JPanel();
		frame.setContentPane(pane);
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		int width = screenSize.width;
		int height = screenSize.height;
		int x = (width - WIDTH) / 2;
		int y = (height - HEIGHT) / 2;
		frame.setLocation(x, y);
		frame.setVisible(true);
		frame.setSize(WIDTH, HEIGHT);
		frame.setResizable(true);
		JButton button1 = new JButton("打开第一个窗口");
		JButton button2 = new JButton("打开第二个窗口");
		JButton button3 = new JButton("打开第三个窗口");
		JButton button4 = new JButton("打开第四个窗口");
		JButton button5 = new JButton("打开第五个窗口");
		JButton button6 = new JButton("打开第六个窗口");
		pane.setLayout(new GridLayout(2, 3));
		pane.add(button1);
		pane.add(button2);
		pane.add(button3);
		pane.add(button4);
		pane.add(button5);
		pane.add(button6);
		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {
				SwingWorker work = new SwingWorker() {

					protected Object doInBackground() throws Exception {
						try {
							sleep(100);
						} catch (Exception e) {
						}
						frame1 f = new frame1();
						return null;
					}

				};
				work.execute();

			}
		});
		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {

				SwingWorker work = new SwingWorker() {

					protected Object doInBackground() throws Exception {
						/*try {
							sleep(10000);
						} catch (Exception e) {
						}*/
						frame2 f = new frame2();
						return null;
					}
				};
				work.execute();
			}
		});
		button3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {
				/*SwingWorker work = new SwingWorker() {

					protected Object doInBackground() throws Exception {
						try {
							sleep(10000);
						} catch (Exception e) {
						}
						frame3 f = new frame3();
						return null;
					}
				};
				work.execute();*/
			}
		});
		button4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {
				/*SwingWorker work = new SwingWorker() {

					protected Object doInBackground() throws Exception {
						try {
							sleep(10000);
						} catch (Exception e) {
						}
						frame4 f = new frame4();
						return null;
					}
				};
				work.execute();*/
			}
		});
		button5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {
				/*SwingWorker work = new SwingWorker() {

					protected Object doInBackground() throws Exception {
						try {
							sleep(10000);
						} catch (Exception e) {
						}
						frame5 f = new frame5();
						return null;
					}
				};
				work.execute();*/
			}
		});
		button6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {

				/*
				 * SwingWorker work=new SwingWorker() {
				 * 
				 * protected Object doInBackground() throws Exception { try {
				 * sleep(10000); } catch(Exception e){} frame6 f=new frame6();
				 * return null; } }; work.execute();
				 */
			}

		});
	}
}

class frame1 
{//创建一个窗口
	frame1()
	{
		JFrame frame=new JFrame("SwingWorker测试程序1");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel pane=new JPanel();
		frame.setContentPane(pane);
		Toolkit kit=Toolkit.getDefaultToolkit();
		Dimension screenSize=kit.getScreenSize();
		int width=screenSize.width;
		int height=screenSize.height;
		int x=(width-mainframe.WIDTH)/2;
		int y=(height-mainframe.HEIGHT)/2;
		frame.setLocation(x,y);
		frame.setVisible(true);
		frame.setResizable(false);
		JButton button1=new JButton("这是第一个窗口的结果");
		pane.add(button1,"Center");
	}
}

class frame2 
{//创建一个窗口
frame2()
{
	JFrame frame=new JFrame("SwingWorker测试程序1");
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	JPanel pane=new JPanel();
	frame.setContentPane(pane);
	Toolkit kit=Toolkit.getDefaultToolkit();
	Dimension screenSize=kit.getScreenSize();
	int width=screenSize.width;
	int height=screenSize.height;
	int x=(width-mainframe.WIDTH)/2;
	int y=(height-mainframe.HEIGHT)/2;
	frame.setLocation(x,y);
	frame.setVisible(true);
	frame.setResizable(false);
	JButton button1=new JButton("这是第一个窗口的结果");
	pane.add(button1,"Center");
}
}